<?php

namespace App\Http\Controllers\Admin;

use App\Models\Admin\Carousel;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Storage;

class CarouselController extends Controller
{
    //轮播图的视图
    public function index(){

        $carousel = new Carousel();
        $carousels=$carousel->orderby('id','asc')->get();
        return view('admin.carousel_img')->with('carousels',$carousels);
    }

    //添加轮播图的视图
    public function create(){

        return view('admin.carousel_img_create');
    }
    //提交图片
    public function store(Request $request){

        $file = $request->imgpath;
//        dd($file);

        $title = $request->comment;
        // 文件是否上传成功
        if ($file->isValid()) {
            // 获取文件相关信息
            $originalName = $file->getClientOriginalName(); // 文件原名
            $ext = $file->getClientOriginalExtension();     // 扩展名
            $realPath = $file->getRealPath();   //临时文件的绝对路径
            $type = $file->getClientMimeType();     // image/jpeg
            // 上传文件
            $filename = date('Y-m-d-H-i-s') . '-' . uniqid() . '.' . $ext;
            // 使用我们新建的uploads本地存储空间（目录）

            $bool = Storage::disk('uploads')->put($filename, file_get_contents($realPath));
        }
        $carousel = new Carousel();
        $carousel->comment = $title;
        $carousel->imgpath = $filename;
        $carousel->save();
        $carousels= $carousel->all();
        return view('admin.carousel_img')->with('carousels',$carousels);
    }
    //修改轮播图图片
    public function edit($id)
    {
        $carousels = Carousel::find($id);

        return view('admin.carousel_img_edit')->with('carousels',$carousels);

    }
    //修改轮播图图片到数据库  提交的按钮  ,修改成功后删除原图
    public function update(Request $request,$id){
        $img = Carousel::find($id);
        $file = $request->imgpath;
        $title = $request->comment;
        if($file == null){
            $img->comment = $title;
            $img->save();
            return redirect('admin/carousel_img_edit');
        }
        // 文件是否上传成功
        if ($file->isValid()) {
            // 删除原图  本地
//            $filePath = public_path().'\uploads\\'.$img->imgpath;
            $filePath = public_path().'/uploads/'.$img->imgpath;
            unlink($filePath);
            // 获取文件相关信息
            $originalName = $file->getClientOriginalName(); // 文件原名
            $ext = $file->getClientOriginalExtension();     // 扩展名
            $realPath = $file->getRealPath();   //临时文件的绝对路径
            $type = $file->getClientMimeType();     // image/jpeg
            $username = $request->username;
            $specialname = $request->specialname;
            //$pic = $request->pic->store('images');
            $city = $request->city;
            // 上传文件
            $filename = date('Y-m-d-H-i-s') . '-' . uniqid() . '.' . $ext;
            // 使用我们新建的uploads本地存储空间（目录）
            $bool = Storage::disk('uploads')->put($filename, file_get_contents($realPath));
        }
        $img = Carousel::find($id);
        $img->comment = $title;
        $img->imgpath = $filename;
        $img->save();
        return redirect('admin/admin_carousel');
    }

//    删除轮播图+同时删除保存路径的原图
    public function destroy($id){

        $img = Carousel::find($id);
        $img->delete();
        //删除图片  本地
//        $fileName = public_path().'\uploads\\'.$img->imgpath;
        $fileName = public_path().'/uploads/'.$img->imgpath;
        unlink($fileName);
        return redirect('admin/admin_carousel');
    }


}

